import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/material.dart';
import 'package:flutter_hsh/fluttergromore/ads/ads_utils.dart';
import 'package:flutter_hsh/routes/app_pages.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_hsh/routes/my_route_observer.dart';
import 'package:flutter_hsh/utils/k_log.dart';
import 'package:flutter_hsh/widgets/keep_alive_wrapper.dart';
import 'package:flutter_localization/flutter_localization.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class AppView extends StatefulWidget {
  const AppView({Key? key}) : super(key: key);

  @override
  State<AppView> createState() => _AppViewState();
}

class _AppViewState extends State<AppView> {
  final FlutterLocalization _localization = FlutterLocalization.instance;

  @override
  void initState() {
    _localization.init(
      mapLocales: [const MapLocale('zh', {})],
      initLanguageCode: 'zh',
    );
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    final botToastBuilder = BotToastInit();
    return ScreenUtilInit(
      designSize: const Size(375, 812),
      minTextAdapt: true,
      splitScreenMode: true,
      builder: (_, child) => GetMaterialApp(
        debugShowCheckedModeBanner: false,
        builder: (context, child) {
          child = FlutterEasyLoading(
            child: KeepAliveWrapper(
              child: MediaQuery(
                //设置文字大小不随系统设置改变
                data: MediaQuery.of(context)
                    .copyWith(textScaler: const TextScaler.linear(1.0)),
                child: GestureDetector(
                  onTap: () => hideKeyboard(context),
                  child: child,
                ),
              ),
            ),
          );
          child = botToastBuilder(context, child);
          return child;
        },
        //home: HomePage(),
        initialRoute: Routes.splash,
        defaultTransition: Transition.cupertino,
        getPages: AppPages.pages,
        supportedLocales: _localization.supportedLocales,
        localizationsDelegates: [
          RefreshLocalizations.delegate,
          ..._localization.localizationsDelegates
        ],
        // 注册路由观察者
        navigatorObservers: [
          MyRouteObserver(), // 添加自定义观察者
        ],
        routingCallback: (Routing? routing) {
          //if (routing?.current != Routes.main && routing?.isBack == true) {
          // KLog.d("返回界面显示广告 ${routing?.isDialog} ${routing?.isBottomSheet} ${routing?.previous}");
          // if (routing?.isDialog == false &&
          //     routing?.current != null &&
          //     routing?.current != "" &&
          //     routing?.current != "/main" &&
          //     routing?.current != "/splash" &&
          //     routing?.isBack == true) {
          //   // 显示广告
          //   AdsUtils.showInterstitialAd();
          // }
        },
      ),
    );
  }
}

void hideKeyboard(BuildContext? context) {
  if (null != context) {
    FocusScopeNode currentFocus = FocusScope.of(context);
    if (!currentFocus.hasPrimaryFocus && currentFocus.focusedChild != null) {
      FocusManager.instance.primaryFocus!.unfocus();
    }
  }
}
